/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.repository;

import me.zhengjie.pojo.sqlServer.PPTaskResult;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.time.LocalDateTime;

/**
* @website https://eladmin.vip
* @author xinglin
* @date 2024-08-03
**/
public interface PPTaskResultRepository extends JpaRepository<PPTaskResult, Integer>, JpaSpecificationExecutor<PPTaskResult> {

    @Query("select t from PPTaskResult t where t.wcsTaskType=1 and t.status !=2 and t.status !=3")
    PPTaskResult findInBoundTask();

    @Query("select t from PPTaskResult t where t.wcsTaskType=2 and t.status !=2 and t.status !=3")
    PPTaskResult findOutBoundTask();

    @Query("select t from PPTaskResult t where t.wcsTaskType=3 and t.status !=2 and t.status !=3")
    PPTaskResult findMoveTask();

    @Modifying
    @Query("update PPTaskResult tr set tr.fromLocation=?1, tr.toLocation=?2, tr.status=?3,  tr.lastUpdated=?4 where tr.wmsTaskId=?5")
    void updateTaskResult(String fromLocation, String toLocation, Integer status, LocalDateTime lastUpdated, String wmsTaskId);


    @Query("select  pt from PPTaskResult pt where pt.wcsTaskType =?1")
    PPTaskResult findPPTaskResultByWcsTaskType(Integer taskType);

    PPTaskResult findPPTaskResultByWmsTaskId(String taskId);

    @Modifying
    @Transactional
    @Query("delete from PPTaskResult tr where tr.wcsTaskType=?1")
    int deleteByWcsTaskType(Integer taskType);

    @Modifying
    @Transactional
    @Query("delete from PPTaskResult tr where tr.wmsTaskId=?1")
    int deletePPTaskResultByWmsTaskId(String taskId);


    PPTaskResult findPPTaskResultBylpnCode(String palletCode);

    PPTaskResult findPPTaskResultByToLocation(String toLocation);


    @Modifying
    @Query("select t from PPTaskResult t where t.toLocation=?1")
    PPTaskResult findPPTaskResultByDetination(String fromLocation);
}